%REM
	Agent bak
	Description: Comments for Agent
%END REM
Option Public
Option Declare

Sub Initialize
	'/**
	' * 定时运行备份当前数据库设计与文档（非同步方式）
	' * 注意：UNID会变化
	' * @param 参数名 参数说明
	' * @return 返回类型 返回说明
	' * @depends
	' * @see
	' * @error
	' * @version 1.0
	' * @author
	' */
try:
	'set up error handler
	On Error GoTo catch	
	
	Const COPY_DOCUMENT = False'是否备份文档
	
	Dim session As New NotesSession
	Dim dbCurrent As NotesDatabase
	Dim dbTarget As NotesDatabase'目标数据库
	Dim strTargetDBName As String'目标路径
	Dim strTargetServerName As String'目标服务器
	
	MsgBox |Start Backup Schedule!|

	Set dbCurrent = session.Currentdatabase
	strTargetServerName = ""
	strTargetDBName = getCurrentPlainDateString() & "bak\" & dbCurrent.Filepath
	
	Set dbTarget = dbCurrent.CreateCopy( strTargetServerName, strTargetDBName )
	
	If COPY_DOCUMENT Then
		Call CopyAllDocument(dbTarget, dbCurrent)
	End If
	
	MsgBox |Backup Schedule Success to | &  strTargetServerName & | | & strTargetDBName
	
	GoTo finally
catch:
	'log error
	'Msgbox Erl & ":" & Error
	MsgBox LSI_Info(2) & " called by " & LSI_Info(12) & "(" & LSI_Info(430) & "): " & Error$ & " - Line: " & CStr(Erl)
	'resume to the final thing
	Resume finally
	
finally:
	'do the final thing
	Exit Sub
End Sub
%REM
	Function getCurrentPlainDateString
	Description: Comments for Function
%END REM
Function getCurrentPlainDateString() As String
	getCurrentPlainDateString = Format(Now, "yyyymmddhhnnss")
End Function
Function CopyAllDocument(dbTarget As NotesDatabase, dbSource As NotesDatabase)As Boolean
	'/**
	' * 复制数据库文档
	' * 
	' * @param 参数名 参数说明
	' * @return 返回类型 返回说明
	' * @depends
	' * @see
	' * @error
	' * @version 1.0
	' * @author
	' */
try:
	'set up error handler
	On Error GoTo catch
	
	CopyAllDocument = False
	
	Dim collection As NotesDocumentCollection
	Dim doc As NotesDocument
	
	MsgBox "START Copy Database Document：" & dbTarget.Server & |:| & dbTarget.Filepath & |<--| & dbSource.Filepath
	
	Set collection = dbSource.AllDocuments
	Set doc = collection.GetFirstDocument()
	While Not(doc Is Nothing)
		Call doc.CopyToDatabase( dbTarget )
		Set doc = collection.GetNextDocument(doc)
	Wend

	CopyAllDocument = True
	
	MsgBox "END Copy Database Document " & collection.Count & "：" & dbTarget.Server & |:| & dbTarget.Filepath & |<--| & dbSource.Filepath
	
	
	GoTo finally
catch:
	'log error
	'Msgbox Erl & ":" & Error
	MsgBox LSI_Info(2) & " called by " & LSI_Info(12) & "(" & LSI_Info(430) & "): " & Error$ & " - Line: " & CStr(Erl)
	'resume to the final thing
	Resume finally

finally:
	'do the final thing
	Exit Function
End Function